<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	public $settings_site;
	public function __construct(){
		parent::__construct();
		$this->load->model('login_m');
		$this->settings_site = $this->user_m->settings_site();
		if (!$this->session->userdata('user_name') && get_cookie('user_name') && get_cookie('remember_code')){
			$this->login_m->login_remembered_user();
		}
	}
	
	/*默认方法*/
	public function index(){
		if($this->session->userdata('user_name')){
			redirect('app');
		}else{
			$this->load->view('login');
		}
	}

	//登录
	function _do_post(){
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username', '用户名', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', '密码', 'trim|required|xss_clean');
		
		if ($this->form_validation->run() == FALSE){
			$this->session->set_flashdata('error', validation_errors());
			redirect('login');			
		}else{			
			if($this->login_m->login($this->input->post('username',TRUE), $this->input->post('password',TRUE), $this->input->post('remember',TRUE))){
				redirect('app');
			}else{
				$this->session->set_flashdata('error','用户名或密码不正确！');
				redirect('login');
			}
		}
	}

	//退出
	function quit(){
		$array_items = array('user_name'=>'','user_id'=>'','user_actual'=>'','role_id'=>'','role_name'=>'','dept_id'=>'','dept_name'=>'');
		$this->session->unset_userdata($array_items);

		//delete the remember me cookies if they exist
		if (get_cookie('user_name')){
			delete_cookie('user_name');
		}
		if (get_cookie('remember_code')){
			delete_cookie('remember_code');
		}
		redirect('login');
	}
	
	function reset_to_home(){
		echo "<script>top.location='".base_url('login')."'</script>";	
	}
}
?>